package cn.hpclub.server.controller.admin;

import java.sql.SQLException;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.tx.Tx;

import cn.hpclub.server.model.SysCodeType;
import cn.hpclub.server.util.InterfaceUtil;

public class SysCodeTypeController extends BaseAdminController<SysCodeType>{

	public void list(){
		ConcurrentHashMap<String, String> searchInfo = getSearchInfo();

		SysCodeType code = new SysCodeType();
        findByPageForSearch(code.getSelect(), code.getSqlExceptSelect(searchInfo));
        setAttrs(searchInfo);

        render("/admin/sys_code_type.tpl");
    }
	
	public void add(){
		setAttr("codeTypeID", -1);

        render("/admin/sys_code_type_input.tpl");
	}
	
	/**
     * @Title: save
     * @Description: 新增/编辑时的保存 (id为-1时为新增)
     */
    public void save(){
        int id = getParaToInt("codeTypeID");

        if(-1 == id){
        	if(!isExist(getPara("typeCode"))){
        		addTypeCode();
        	}else{
        		this.setAttr("errorMessage", "编码CODE重复，请重新输入编码");
                render("/common/error.tpl");
                return;
        	}
        } else{
            editTypeCode();
        }

        redirect("/sysCodeType/list");
    }

    /**
     * @Title: edit
     * @Description: 编辑
     */
    public void edit(){
        String id = getPara("id");

        SysCodeType code = SysCodeType.dao.findById(id);

        setAttr("codeTypeID", code.getInt("id"));
        setAttr("typeName", code.getStr("name"));
        setAttr("typeCode", code.getStr("code"));
        setAttr("typeValue", code.getStr("value"));
        setAttr("typeSort", code.getInt("sort"));
        setAttr("is_deleted", code.getBoolean("is_deleted"));
        setAttr("typeRemark", code.getStr("remark"));

        render("/admin/sys_code_type_input.tpl");
    }

    /**
     * @Title: addTypeCode
     * @Description:新增资源
     */
    @Before(Tx.class)
    public void addTypeCode(){
    	SysCodeType code = new SysCodeType();

        code.set("name", getPara("typeName"));
        code.set("code", getPara("typeCode"));
        code.set("value", getPara("typeValue"));
        code.set("sort", getPara("typeSort"));
        code.set("is_deleted", getParaToBoolean("is_deleted"));
        code.set("remark", getPara("typeRemark"));
        code.set("created_by", InterfaceUtil.getAdminId());
        code.set("created_time", new Date());

        code.save();
    }
    
    private boolean isExist(String code){
    	String sql = "select count(*) as cnt from sys_code_type where code = '" + code + "'";
    	Long cnt = Db.queryLong(sql);
    	
    	return (cnt > 0) ? true : false;
    }

    public void editTypeCode(){
    	SysCodeType code = new SysCodeType();

        code.set("id", getPara("codeTypeID"));
        code.set("name", getPara("typeName"));
        code.set("code", getPara("typeCode"));
        code.set("value", getPara("typeValue"));
        code.set("sort", getPara("typeSort"));
        code.set("is_deleted", getParaToBoolean("is_deleted"));
        code.set("remark", getPara("typeRemark"));
        code.set("last_updated_by", InterfaceUtil.getAdminId());
        code.set("last_updated_time", new Date());

        code.update();
    }

    /**
     * @Title: delete
     * @Description: 删除
     */
    @Before(Tx.class)
    public void delete() throws SQLException{
        String[] ids = getParaValues("ids");
        if(ids == null || ids.length <= 0){
            ajaxJsonErrorMessage("删除失败，无选中项");
        }

        boolean deleted = deleteCodeTypeByIDs(ids);

        if(deleted){
            ajaxJsonSuccessMessage("删除成功！");
        } else{
            ajaxJsonErrorMessage("删除失败！");
            throw new SQLException("基础数据(大类)删除失败");
        }
    }

    private boolean deleteCodeTypeByIDs(String[] ids){
        boolean deleted = true;
        for(String id : ids){
        	SysCodeType code = new SysCodeType();
            code.set("id", id);
            code.set("is_deleted", 1);
            code.set("last_updated_by", InterfaceUtil.getAdminId());
            code.set("last_updated_time", new Date());
            if(!code.update()){
                deleted = false;
                break;
            }
        }

        return deleted;
    }
}
